package com.openingedu.mba.screen.activity.practice;

import java.math.BigDecimal;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.lurencun.android.support.ui.BackUIActivity;
import com.openingedu.mba.R;
import com.openingedu.mba.common.AppSetting;
import com.openingedu.mba.common.CommonUtil;
import com.openingedu.mba.persist.DbHistory;
import com.openingedu.mba.service.PracticeService;
import com.openingedu.mba.util.TimeUtil;

/**
 * 练习完成后结果界面
 * @author unidw
 *
 */
public class ResultActivity extends BackUIActivity {

	private Context mContext;
	private PracticeService practiceService;
	private String title;
	private long elapsedMillis;
	
	private int correctNum = 0;
	private int faultNum = 0;
	private int totalNum = 0;
	
	private int perScore = 2;
	private TextView totalUsedTimeView;
	private TextView averageUsedTimeView;
	private TextView correctNumView;
	private TextView faultNumView;
	private TextView scoreNumView;
	private TextView valuationView;
	private Button finishBtn;
	
	@Override
	protected void onCreateEx(Bundle savedInstanceState) {
		mContext = this;
		practiceService = new PracticeService();
		Intent intent = getIntent();
		title = intent.getStringExtra(AppSetting.MENU_TITLE);
		
		elapsedMillis = intent.getLongExtra("elapsedMillis", 0L);
		correctNum = intent.getIntExtra("correctNum", 0);
		faultNum = intent.getIntExtra("faultNum", 0);
		totalNum = intent.getIntExtra("totalNum", 0);
		int score = correctNum * perScore;
		String timeUsed = TimeUtil.parseTime(elapsedMillis/1000);
		String averageTimeUsed = TimeUtil.parseTime(elapsedMillis/1000/totalNum);
		
		totalUsedTimeView = (TextView) findViewById(R.id.total_used_time_view);
		averageUsedTimeView = (TextView) findViewById(R.id.average_used_time_view);
		correctNumView = (TextView) findViewById(R.id.correct_num_view);
		faultNumView = (TextView) findViewById(R.id.fault_num_view);
		scoreNumView = (TextView) findViewById(R.id.score_view);
		valuationView = (TextView) findViewById(R.id.val_content_view);
		finishBtn = (Button) findViewById(R.id.finish_btn);
		totalUsedTimeView.setText(timeUsed);
		averageUsedTimeView.setText(averageTimeUsed);
		correctNumView.setText("" + correctNum + "题");
		faultNumView.setText("" + faultNum + "题");
		scoreNumView.setText("" + score + "分，正确率:" + CommonUtil.percentStr(correctNum, totalNum));
		
		double pd = CommonUtil.percent(correctNum, totalNum);
		int val = computeValuation(pd);
		// 设置标题
		TextView titleView = (TextView) findViewById(R.id.app_title);
		titleView.setText(title);
		finishBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		// 保存练习记录
		DbHistory history = new DbHistory();
		history.setAverageTimeUsed(averageTimeUsed);
		history.setCorrectNum(correctNum);
		history.setFaultNum(faultNum);
		history.setPerScore(perScore);
		history.setPracticeDate(new Date());
		history.setTimeUsed(timeUsed);
		history.setTotalNum(totalNum);
		history.setTotalScore(score);
		// TODO 随即考试 类型
		history.setType(0);
		history.setValuation(val);
		
		practiceService.persistHistory(mContext, history);
	}
	
	/**
	 * 计算评价正确率
	 */
	private int computeValuation(double pd) {
		// 100 PERFECT
		BigDecimal pdBD = new BigDecimal(pd * 100);
		BigDecimal bd100 = new BigDecimal(100);
		BigDecimal bd80 = new BigDecimal(80);
		BigDecimal bd60 = new BigDecimal(60);
		BigDecimal bd40 = new BigDecimal(40);
		BigDecimal bd0 = new BigDecimal(0);
		if (pdBD.compareTo(bd100) == 0) {
			valuationView.setText("完美！您的答案全部正确！");
			return AppSetting.Valuation.PERFECT;
		}
		// 80-100 GOOD
		else if (pdBD.compareTo(bd80) >= 0 && pdBD.compareTo(bd100) < 0) {
			valuationView.setText("太棒了！您太优秀了！");
			return AppSetting.Valuation.GOOD;
		}
		// 60-80 PASS
		else if (pdBD.compareTo(bd60) >= 0 && pdBD.compareTo(bd80) < 0) {
			valuationView.setText("恭喜！您顺利完成了联系！");
			return AppSetting.Valuation.PASSING;
		}
		// 40-60 BAD
		else if (pdBD.compareTo(bd40) >= 0 && pdBD.compareTo(bd60) < 0) {
			valuationView.setText("革命尚未成功，同志仍需努力");
			return AppSetting.Valuation.BAD;
		}
		// 0-40 POOR
		else if (pdBD.compareTo(bd0) >= 0 && pdBD.compareTo(bd40) < 0) {
			valuationView.setText("好吧，合理安排时间吧！");
			return AppSetting.Valuation.POOR;
		}
		return AppSetting.Valuation.NONE;
	}

	@Override
	protected int getBackButtonResId() {
		return R.id.back_button;
	}

	@Override
	protected int getContentViewLayoutId() {
		return R.layout.practice_result;
	}

	@Override
	protected boolean isConfirmBack() {
		return false;
	}

}
